﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using KPIS.GERP.GHRMS.DAL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.IDAL;
using KPIS.GERP.GHRMS.DALFactory;

namespace KPIS.GERP.GHRMS.BLL
{
    public class AbsenceRuleBLL : IAbsenceRuleBLL
    {
        private static readonly IAbsenceRuleDAL DAL = DataAccess.CreateAbsenceRules();

        private AbsenceRuleInfo ruleInfo = new AbsenceRuleInfo();
        private EmployeeTypeInfo etInfo = new EmployeeTypeInfo();
        private AbsenceTypeInfo abInfo = new AbsenceTypeInfo();

        public IList<rule_absence> GetAbsenceRulesByEmpSeq(string emp_seq)
        {
            return DAL.GetAbsenceRulesByEmpSeq(emp_seq);
        }

        public IList<rule_absence> GetAbsenceRules(string emp_type_seq, string absence_type_seq)
        {
            if (String.IsNullOrEmpty(absence_type_seq))
                return DAL.GetAbsenceRules(emp_type_seq);
            else
                return DAL.GetAbsenceRules(emp_type_seq, absence_type_seq);
        }

        public IList<AbsenceRuleInfo> ListAbsenceRules(string EmployeeType)
        {
            if (EmployeeType.Length > 0)
                etInfo.Sequence = Int32.Parse(EmployeeType);
            ruleInfo.EmployeeType = etInfo;
            return DAL.ListAbsenceRules(ruleInfo);
        }

        public IList<AbsenceRuleInfo> ListAbsenceRulesBySequence(int Sequence)
        {
            return DAL.ListAbsenceRules(new AbsenceRuleInfo(Sequence));
        }

        public void Insert(string EmployeeType, string AbsenceType, decimal QuotaDay, decimal QuotaSalary, decimal QuotaHalfSalary
                           , decimal QuotaCount, decimal DayPerCount, string Remarks, int CreatedBy, bool inclNonWorkingDay
                           , AbsenceRuleInfo.UseSendBeforeEnum useSendBeforeEnum, decimal sendBefore)
        {
            if (EmployeeType.Length > 0)
                etInfo.Sequence = Int32.Parse(EmployeeType);
            if (AbsenceType.Length > 0)
                abInfo.Sequence = Int32.Parse(AbsenceType);

            var model = new AbsenceRuleInfo(0, etInfo, abInfo, QuotaDay, QuotaSalary, QuotaHalfSalary, QuotaCount, DayPerCount
                                            , Remarks, "", CreatedBy, 0, inclNonWorkingDay);
            model.SetSendBefore(useSendBeforeEnum, sendBefore);
            DAL.Insert(model);

        }

        public void Update(int Sequence, string EmployeeType, string AbsenceType, decimal QuotaDay, decimal QuotaSalary, decimal QuotaHalfSalary
                           , decimal QuotaCount, decimal DayPerCount, string Remarks, int UpdatedBy, bool inclNonWorkingDay
                           , AbsenceRuleInfo.UseSendBeforeEnum useSendBeforeEnum, decimal sendBefore)
        {
            if (EmployeeType.Length > 0)
                etInfo.Sequence = Int32.Parse(EmployeeType);
            if (AbsenceType.Length > 0)
                abInfo.Sequence = Int32.Parse(AbsenceType);
            try
            {
                var model = new AbsenceRuleInfo(Sequence, etInfo, abInfo, QuotaDay, QuotaSalary, QuotaHalfSalary, QuotaCount, DayPerCount
                                                , Remarks, "", 0, UpdatedBy, inclNonWorkingDay);
                model.SetSendBefore(useSendBeforeEnum, sendBefore);
                DAL.Update(model);
            }
            catch
            {
                throw new Exception("");
            }
        }

        public void Delete(int Sequence, int UpdatedBy)
        {
            try
            {
                DAL.Delete(Sequence, UpdatedBy);
            }
            catch
            {
                throw new Exception("");
            }
        }
    }
}